#!/bin/sh
###############################################################################
##  Author    : 陈贰浩
##  Name      : edw_hbase.sh
##  Functions : 综合查询数据入库
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2019-02-15  陈贰浩           1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh


V_DATA_DATE=$(grep "#V_DATA_DATE#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
V_FREQUENCY=$(grep "#V_FREQUENCY#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}_${V_PARALLEL_CNT}.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output


BEGIN
LOGGER "INFO" "Today batch date is ${V_DATA_DATE}."
TAB_LIST=(depb loab loaf)
## 1 综合查询入库
LOGGER "INFO" "HBASE数据入库开始."
for TAB in ${TAB_LIST[@]}
do
    LOGGER "INFO" "${TAB}表开始入库."
    del_tab="delete from ${TAB} where data_month='${V_DATA_DATE:0:6}'"
    sqlline.py << EOF
${del_tab};
!q
EOF
    RE_CODE=$?
    if [[ ${RE_CODE} -ne 0 ]]; then
        LOGGER "ERROR" "表${TAB}清除历史失败."
        LOGGER "DEBUG" "SQL->${del_tab}."
        exit 1
    fi
    cd /home/hadoop/dwhdp/core/phoenix && hadoop jar phoenix-4.13.1-HBase-1.1-client.jar org.apache.phoenix.mapreduce.CsvBulkLoadTool -t ${TAB} -d ',' -i /user/phonenix/${TAB}.csv -z hadoop01,hadoop02,hadoop03:2181
    RE_CODE=$?
    if [[ ${RE_CODE} -ne 0 ]]; then
        LOGGER "ERROR" "表${TAB}入hbase库失败."
        exit 1
    fi
    LOGGER "INFO" "${TAB}表入库成功."
done
LOGGER "INFO" "HBASE数据入库完成."
END